/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JCashBookEditor.java
 *
 * Created on 2010-2-4, 12:09:59
 */

package cn.ekuma.epos.finance;

import cn.ekuma.data.dao.I_ParentNode;
import cn.ekuma.data.dao.bean.IKeyed;
import cn.ekuma.data.ui.swing.ParentNodeChangeListener;
import cn.ekuma.epos.bean.util.PaymentsModelHelper;
import cn.ekuma.epos.datalogic.I_DataLogicSales;

import com.openbravo.bean.CloseCash;
import com.openbravo.bean.PaymentsModel;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.TableRendererBasic;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.base.BeanFactoryException;
import com.openbravo.pos.panels.event.BaseBeanChangedListener;
import com.openbravo.pos.panels.model.PaymentsTableModel;
import com.openbravo.pos.panels.model.SalesTableModel;
import java.awt.Dimension;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import java.awt.GridLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
/**
 *
 * @author Administrator
 */
public class JCashBookLine extends javax.swing.JPanel implements  BaseBeanChangedListener<CloseCash,String>{
    private PaymentsModel m_PaymentsToClose = null;
    String currentCashIndex;
	private I_DataLogicSales dlSales;

    /** Creates new form JCashBookEditor */
    public JCashBookLine() {
        initComponents();
    }

     public void init(I_DataLogicSales dlSales) throws BeanFactoryException {
    	this.dlSales=dlSales;
        m_jTicketTable.setDefaultRenderer(Object.class, new TableRendererBasic(
                new Formats[] {new FormatsPayment(), Formats.CURRENCY}));
        m_jTicketTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        m_jScrollTableTicket.getVerticalScrollBar().setPreferredSize(new Dimension(25,25));
        m_jTicketTable.getTableHeader().setReorderingAllowed(false);
        m_jTicketTable.setRowHeight(25);
        m_jTicketTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        m_jsalestable.setDefaultRenderer(Object.class, new TableRendererBasic(
                new Formats[] {Formats.STRING, Formats.CURRENCY, Formats.CURRENCY, Formats.CURRENCY}));
        m_jsalestable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        m_jScrollSales.getVerticalScrollBar().setPreferredSize(new Dimension(25,25));
        m_jsalestable.getTableHeader().setReorderingAllowed(false);
        m_jsalestable.setRowHeight(25);
        m_jsalestable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
    	setLayout(new GridLayout(1, 2, 0, 0));
    	jPanel6 = new javax.swing.JPanel();
    	add(jPanel6);
    	m_jSalesTotal = new javax.swing.JTextField();
    	m_jScrollSales = new javax.swing.JScrollPane();
    	m_jsalestable = new javax.swing.JTable();
    	m_jSalesTaxes = new javax.swing.JTextField();
    	m_jSalesSubtotal = new javax.swing.JTextField();
    	m_jSales = new javax.swing.JTextField();
    	jLabel5 = new javax.swing.JLabel();
    	jLabel6 = new javax.swing.JLabel();
    	jLabel12 = new javax.swing.JLabel();
    	jLabel7 = new javax.swing.JLabel();
    	
    	        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(AppLocal.getIntString("label.salestitle"))); // NOI18N
    	        
    	                m_jSalesTotal.setEditable(false);
    	                m_jSalesTotal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
    	                
    	                        m_jsalestable.setFocusable(false);
    	                        m_jsalestable.setIntercellSpacing(new java.awt.Dimension(0, 1));
    	                        m_jsalestable.setRequestFocusEnabled(false);
    	                        m_jsalestable.setShowVerticalLines(false);
    	                        m_jScrollSales.setViewportView(m_jsalestable);
    	                        
    	                                m_jSalesTaxes.setEditable(false);
    	                                m_jSalesTaxes.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
    	                                
    	                                        m_jSalesSubtotal.setEditable(false);
    	                                        m_jSalesSubtotal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
    	                                        
    	                                                m_jSales.setEditable(false);
    	                                                m_jSales.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
    	                                                
    	                                                        jLabel5.setText(AppLocal.getIntString("label.sales")); // NOI18N
    	                                                        
    	                                                                jLabel6.setText(AppLocal.getIntString("label.subtotalcash")); // NOI18N
    	                                                                
    	                                                                        jLabel12.setText(AppLocal.getIntString("label.taxcash")); // NOI18N
    	                                                                        
    	                                                                                jLabel7.setText(AppLocal.getIntString("label.totalcash")); // NOI18N
    	                                                                                
    	                                                                                        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
    	                                                                                        jPanel6Layout.setHorizontalGroup(
    	                                                                                        	jPanel6Layout.createParallelGroup(Alignment.LEADING)
    	                                                                                        		.addGroup(jPanel6Layout.createSequentialGroup()
    	                                                                                        			.addContainerGap()
    	                                                                                        			.addComponent(m_jScrollSales, GroupLayout.PREFERRED_SIZE, 222, GroupLayout.PREFERRED_SIZE)
    	                                                                                        			.addPreferredGap(ComponentPlacement.RELATED)
    	                                                                                        			.addGroup(jPanel6Layout.createParallelGroup(Alignment.LEADING)
    	                                                                                        				.addGroup(jPanel6Layout.createSequentialGroup()
    	                                                                                        					.addComponent(jLabel5, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
    	                                                                                        					.addPreferredGap(ComponentPlacement.RELATED)
    	                                                                                        					.addComponent(m_jSales, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE))
    	                                                                                        				.addGroup(jPanel6Layout.createSequentialGroup()
    	                                                                                        					.addComponent(jLabel6, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
    	                                                                                        					.addPreferredGap(ComponentPlacement.RELATED)
    	                                                                                        					.addComponent(m_jSalesSubtotal, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE))
    	                                                                                        				.addGroup(jPanel6Layout.createSequentialGroup()
    	                                                                                        					.addComponent(jLabel12, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
    	                                                                                        					.addPreferredGap(ComponentPlacement.RELATED)
    	                                                                                        					.addComponent(m_jSalesTaxes, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE))
    	                                                                                        				.addGroup(jPanel6Layout.createSequentialGroup()
    	                                                                                        					.addComponent(jLabel7, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
    	                                                                                        					.addPreferredGap(ComponentPlacement.RELATED)
    	                                                                                        					.addComponent(m_jSalesTotal, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)))
    	                                                                                        			.addContainerGap(128, Short.MAX_VALUE))
    	                                                                                        );
    	                                                                                        jPanel6Layout.setVerticalGroup(
    	                                                                                        	jPanel6Layout.createParallelGroup(Alignment.LEADING)
    	                                                                                        		.addGroup(jPanel6Layout.createSequentialGroup()
    	                                                                                        			.addGroup(jPanel6Layout.createParallelGroup(Alignment.LEADING)
    	                                                                                        				.addComponent(m_jScrollSales, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE)
    	                                                                                        				.addGroup(jPanel6Layout.createSequentialGroup()
    	                                                                                        					.addGroup(jPanel6Layout.createParallelGroup(Alignment.BASELINE)
    	                                                                                        						.addComponent(jLabel5)
    	                                                                                        						.addComponent(m_jSales, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    	                                                                                        					.addPreferredGap(ComponentPlacement.RELATED)
    	                                                                                        					.addGroup(jPanel6Layout.createParallelGroup(Alignment.BASELINE)
    	                                                                                        						.addComponent(jLabel6)
    	                                                                                        						.addComponent(m_jSalesSubtotal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    	                                                                                        					.addPreferredGap(ComponentPlacement.RELATED)
    	                                                                                        					.addGroup(jPanel6Layout.createParallelGroup(Alignment.BASELINE)
    	                                                                                        						.addComponent(jLabel12)
    	                                                                                        						.addComponent(m_jSalesTaxes, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    	                                                                                        					.addPreferredGap(ComponentPlacement.RELATED)
    	                                                                                        					.addGroup(jPanel6Layout.createParallelGroup(Alignment.BASELINE)
    	                                                                                        						.addComponent(jLabel7)
    	                                                                                        						.addComponent(m_jSalesTotal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
    	                                                                                        			.addContainerGap(28, Short.MAX_VALUE))
    	                                                                                        );
    	                                                                                        jPanel6.setLayout(jPanel6Layout);
    	jPanel5 = new javax.swing.JPanel();
    	add(jPanel5);
    	m_jScrollTableTicket = new javax.swing.JScrollPane();
    	m_jTicketTable = new javax.swing.JTable();
    	jLabel1 = new javax.swing.JLabel();
    	m_jCount = new javax.swing.JTextField();
    	jLabel4 = new javax.swing.JLabel();
    	m_jCash = new javax.swing.JTextField();
    	jLabel9 = new javax.swing.JLabel();
    	jHandCash = new javax.swing.JTextField();
    	jScrollPane1 = new javax.swing.JScrollPane();
    	
    	        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(AppLocal.getIntString("label.paymentstitle"))); // NOI18N
    	        
    	                m_jScrollTableTicket.setMinimumSize(new Dimension(200, 140));
    	                m_jScrollTableTicket.setPreferredSize(new Dimension(200, 140));
    	                
    	                        m_jTicketTable.setFocusable(false);
    	                        m_jTicketTable.setIntercellSpacing(new java.awt.Dimension(0, 1));
    	                        m_jTicketTable.setRequestFocusEnabled(false);
    	                        m_jTicketTable.setShowVerticalLines(false);
    	                        m_jScrollTableTicket.setViewportView(m_jTicketTable);
    	                        
    	                                jLabel1.setText(AppLocal.getIntString("Label.Tickets")); // NOI18N
    	                                
    	                                        m_jCount.setEditable(false);
    	                                        m_jCount.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
    	                                        
    	                                                jLabel4.setText(AppLocal.getIntString("Label.Cash")); // NOI18N
    	                                                
    	                                                        m_jCash.setEditable(false);
    	                                                        m_jCash.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
    	                                                        
    	                                                                jLabel9.setText(AppLocal.getIntString("label.handedCash")); // NOI18N
    	                                                                
    	                                                                        jHandCash.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
    	                                                                                        jMemo = new javax.swing.JTextPane();
    	                                                                                
    	                                                                                        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
    	                                                                                        jPanel5Layout.setHorizontalGroup(
    	                                                                                        	jPanel5Layout.createParallelGroup(Alignment.LEADING)
    	                                                                                        		.addGroup(jPanel5Layout.createSequentialGroup()
    	                                                                                        			.addContainerGap()
    	                                                                                        			.addComponent(m_jScrollTableTicket, GroupLayout.PREFERRED_SIZE, 235, GroupLayout.PREFERRED_SIZE)
    	                                                                                        			.addPreferredGap(ComponentPlacement.RELATED)
    	                                                                                        			.addGroup(jPanel5Layout.createParallelGroup(Alignment.LEADING)
    	                                                                                        				.addGroup(jPanel5Layout.createSequentialGroup()
    	                                                                                        					.addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
    	                                                                                        					.addPreferredGap(ComponentPlacement.RELATED)
    	                                                                                        					.addComponent(m_jCount, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE))
    	                                                                                        				.addGroup(jPanel5Layout.createSequentialGroup()
    	                                                                                        					.addGroup(jPanel5Layout.createParallelGroup(Alignment.TRAILING, false)
    	                                                                                        						.addComponent(jLabel9, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    	                                                                                        						.addComponent(jLabel4, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
    	                                                                                        					.addPreferredGap(ComponentPlacement.RELATED)
    	                                                                                        					.addGroup(jPanel5Layout.createParallelGroup(Alignment.LEADING, false)
    	                                                                                        						.addComponent(jHandCash)
    	                                                                                        						.addComponent(m_jCash, GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)))
    	                                                                                        				.addComponent(jMemo, GroupLayout.PREFERRED_SIZE, 192, GroupLayout.PREFERRED_SIZE)
    	                                                                                        				.addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE))
    	                                                                                        			.addContainerGap())
    	                                                                                        );
    	                                                                                        jPanel5Layout.setVerticalGroup(
    	                                                                                        	jPanel5Layout.createParallelGroup(Alignment.LEADING)
    	                                                                                        		.addGroup(jPanel5Layout.createSequentialGroup()
    	                                                                                        			.addGroup(jPanel5Layout.createParallelGroup(Alignment.BASELINE)
    	                                                                                        				.addComponent(m_jScrollTableTicket, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE)
    	                                                                                        				.addGroup(jPanel5Layout.createSequentialGroup()
    	                                                                                        					.addGroup(jPanel5Layout.createParallelGroup(Alignment.LEADING)
    	                                                                                        						.addGroup(jPanel5Layout.createSequentialGroup()
    	                                                                                        							.addGap(27)
    	                                                                                        							.addGroup(jPanel5Layout.createParallelGroup(Alignment.BASELINE)
    	                                                                                        								.addComponent(jLabel4)
    	                                                                                        								.addComponent(m_jCash, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    	                                                                                        							.addPreferredGap(ComponentPlacement.RELATED)
    	                                                                                        							.addGroup(jPanel5Layout.createParallelGroup(Alignment.BASELINE)
    	                                                                                        								.addComponent(jLabel9)
    	                                                                                        								.addComponent(jHandCash, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
    	                                                                                        						.addComponent(jLabel1)
    	                                                                                        						.addComponent(m_jCount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    	                                                                                        					.addPreferredGap(ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
    	                                                                                        					.addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    	                                                                                        					.addGap(9)
    	                                                                                        					.addComponent(jMemo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
    	                                                                                        			.addContainerGap(28, Short.MAX_VALUE))
    	                                                                                        );
    	                                                                                        jPanel5.setLayout(jPanel5Layout);
    }// </editor-fold>//GEN-END:initComponents



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField jHandCash;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextPane jMemo;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField m_jCash;
    private javax.swing.JTextField m_jCount;
    private javax.swing.JTextField m_jSales;
    private javax.swing.JTextField m_jSalesSubtotal;
    private javax.swing.JTextField m_jSalesTaxes;
    private javax.swing.JTextField m_jSalesTotal;
    private javax.swing.JScrollPane m_jScrollSales;
    private javax.swing.JScrollPane m_jScrollTableTicket;
    private javax.swing.JTable m_jTicketTable;
    private javax.swing.JTable m_jsalestable;
    // End of variables declaration//GEN-END:variables

  private void loadData() throws BasicException {
      
      m_jCount.setText(null); // AppLocal.getIntString("label.noticketstoclose");
      m_jCash.setText(null);

      m_jSales.setText(null);
      m_jSalesSubtotal.setText(null);
      m_jSalesTaxes.setText(null);
      m_jSalesTotal.setText(null);
      
      m_jTicketTable.setModel(new DefaultTableModel());
      m_jsalestable.setModel(new DefaultTableModel());
          
      // LoadData
      m_PaymentsToClose =dlSales.loadInstance(currentCashIndex);

      if (m_PaymentsToClose.getPayments() != 0 || m_PaymentsToClose.getSales() != 0) {
          m_jCount.setText(PaymentsModelHelper.printPayments(m_PaymentsToClose));
          m_jCash.setText(PaymentsModelHelper.printSalesTotal(m_PaymentsToClose));
          
          m_jSales.setText(PaymentsModelHelper.printSales(m_PaymentsToClose));
          m_jSalesSubtotal.setText(PaymentsModelHelper.printSalesBase(m_PaymentsToClose));
          m_jSalesTaxes.setText(PaymentsModelHelper.printSalesTaxes(m_PaymentsToClose));
          m_jSalesTotal.setText(PaymentsModelHelper.printSalesTotal(m_PaymentsToClose));
      }          
      
      m_jTicketTable.setModel(new PaymentsTableModel(m_PaymentsToClose.getM_lpayments()));
              
      TableColumnModel jColumns = m_jTicketTable.getColumnModel();
      jColumns.getColumn(0).setPreferredWidth(150);
      jColumns.getColumn(0).setResizable(false);
      jColumns.getColumn(1).setPreferredWidth(100);
      jColumns.getColumn(1).setResizable(false);
      
      m_jsalestable.setModel(new SalesTableModel(m_PaymentsToClose.getM_lsales()));
      
      jColumns = m_jsalestable.getColumnModel();
      jColumns.getColumn(0).setPreferredWidth(150);
      jColumns.getColumn(0).setResizable(false);
      jColumns.getColumn(1).setPreferredWidth(100);
      jColumns.getColumn(1).setResizable(false);
  }   
    private class FormatsPayment extends Formats {
        @Override
        protected String formatValueInt(Object value) {
            return AppLocal.getIntString("transpayment." + (String) value);
        }
        protected Object parseValueInt(String value) throws ParseException {
            return value;
        }
        public int getAlignment() {
            return javax.swing.SwingConstants.LEFT;
        }
    }

	@Override
	public void baseBeanChanged(IKeyed<String> key, CloseCash obj) {
		try {
            this.currentCashIndex=key.getKey();
            loadData();
        } catch (BasicException ex) {
            Logger.getLogger(JCashBookLine.class.getName()).log(Level.SEVERE, null, ex);
        }
	}

}
